
#include<iostream>
#include<string>
#define Max 500
using namespace std;
struct person
{
	string m_Name;
	int m_Sex;
	int m_Age;
	string m_Phone;
	string m_Adr;
};
struct AdressBooks
{
	struct person personArr[Max];//创建联系人数组
	int m_Size = 0;//当前通讯录里的联系人
};
void menu()
{
	cout << "*********************************" << endl;
	cout << "**********1，添加联系人**********" << endl;
	cout << "**********2，显示联系人**********" << endl;
	cout << "**********3，删除联系人**********" << endl;
	cout << "**********4，查找联系人**********" << endl;
	cout << "**********5，修改联系人**********" << endl;
	cout << "**********6，清空联系人**********" << endl;
	cout << "**********0，退出通讯录**********" << endl;
	cout << "*********************************" << endl;
}
//1,添加联系人
void Addperson(AdressBooks* abs)
{
	if (abs->m_Size == Max)
	{
		cout << "通讯录已满，无法添加！" << endl;
	}
	else
	{   //添加姓名
		cout << "请输入名字：" << endl;
		string name;
		cin >> name;
		abs->personArr[abs->m_Size].m_Name = name;
		//添加性别
		cout << "请输入性别：1--男 ， 2--女" << endl;
		int sex=0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArr[abs->m_Size].m_Sex = sex;
			}
			else
			{
				cout << "输入错误，请重新输入" << endl;
			}
			break;
		}
		//添加年龄
		cout << "请输入年龄：" << endl;
		int age;
		cin >> age;
		abs->personArr[abs->m_Size].m_Age = age;
		//添加号码
		cout << "请输入号码：" << endl;
		string phone;
		cin >> phone;
		abs->personArr[abs->m_Size].m_Phone = phone;
		//添加住址
		cout << "请输入住址：" << endl;
		string adr;
		cin >> adr;
		abs->personArr[abs->m_Size].m_Adr = adr;
		abs->m_Size++;
		cout << "添加成功" << endl;
		system("system");
		system("cls");
	}
 }
//2，显示联系人
void show( AdressBooks* abs)
{
	if (abs->m_Size == 0)
	{
		cout << "联系人为空，请先添加联系人！" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_Size; i++)
		{
			cout << "姓名：" << abs->personArr[i].m_Name << "\t";
			cout << "性别：" << (abs->personArr[i].m_Sex==1?"男":"女") << "\t";
			cout << "年龄：" << abs->personArr[i].m_Age << "\t";
			cout << "号码：" << abs->personArr[i].m_Phone << "\t";
			cout << "住址：" << abs->personArr[i].m_Adr << "\t";
			cout << "\n";

		}
	}
	system("pause");
	system("cls");
}
//判断一个人是否存在
int isExist(AdressBooks* abs, string name)
{
	for (int i = 0; i < abs->m_Size; i++)
	{
		if (abs->personArr[i].m_Name == name)
		{
			return i;
		}
	}
	return -1;
}
//3删除联系人
void Deleate(AdressBooks * abs)
{
	cout << "请输入你要删除的人的名字" << endl;
	string name;
	cin >> name;
	int ret=isExist(abs, name);
	if (ret != -1)
	{
		for (int i = ret; i < abs->m_Size; i++)
		{
			abs->personArr[i] = abs->personArr[i + 1];
			abs->m_Size--;
		}
		cout << "删除成功" << endl;

	}
	else
	{
		cout << "查无此人" << endl;
	}

	system("pause");
	system("cls");
}
//4,查找联系人
void Seek(AdressBooks* abs)
{
	cout << "请输入你要查找的人的名字" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1)
	{
		cout << "姓名：" << abs->personArr[ret].m_Name << "\t";
		cout << "性别：" << (abs->personArr[ret].m_Sex == 1 ? "男" : "女") << "\t";
		cout << "年龄：" << abs->personArr[ret].m_Age << "\t";
		cout << "号码：" << abs->personArr[ret].m_Phone << "\t";
		cout << "住址：" << abs->personArr[ret].m_Adr << "\t";

	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}
//5,修改联系人
void Gai(AdressBooks* abs)
{
	cout << "请输入要修改名字：" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1)
	{
		//修改姓名
		string name;
		cout << "请输入要修改名字：" << endl;
		cin >> name;
		abs->personArr[ret].m_Name = name;
		//修改性别
		cout << "请输入性别：1--男 ， 2--女" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArr[ret].m_Sex = sex;
			}
			else
			{
				cout << "输入错误，请重新输入" << endl;
			}
			break;
		}
		//修改年龄
		cout << "请输入年龄：" << endl;
		int age;
		cin >> age;
		abs->personArr[ret].m_Age = age;
		//修改号码
		cout << "请输入号码：" << endl;
		string phone;
		cin >> phone;
		abs->personArr[ret].m_Phone = phone;
		//修改住址
		cout << "请输入住址：" << endl;
		string adr;
		cin >> adr;
		abs->personArr[ret].m_Adr = adr;
		abs->m_Size++;
		cout << "添加成功" << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}

	system("system");
	system("cls");
}
//6，清空联系人
void Qing(AdressBooks* abs)
{
	abs->m_Size = 0;
	cout << "通讯录已经清空" << endl;
	system("system");
	system("cls");
}
int main()
{
	//创建通讯录结构体变量
	struct AdressBooks abs;
	int select = 0;
	while (true)
	{
		menu();
		cin >> select;
		switch (select)
		{
		case 1:
			Addperson(&abs);//调用函数实现添加联系人
			break;
		case 2:
			show(&abs);//调用函数实现显示联系人
			break;
		case 3:
			Deleate(&abs);
			break;
		case 4:
			Seek(&abs);
			break;
		case 5:
			Gai(&abs);
			break;
		case 6:
			Qing(&abs);
			break;
		case 0:
			cout << "欢迎下次使用！" << endl;
			system("pause");
			return 0;
			break;
		}

	}

}